package com.aidex.biz.service.impl;

import com.aidex.biz.domain.JinjianFiles;
import com.aidex.biz.domain.ProductFiles;
import com.aidex.biz.mapper.ProductFilesMapper;
import com.aidex.biz.service.JinjianFilesService;
import com.aidex.biz.service.ProductFilesService;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.common.utils.NumberUtils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品资料Service业务层处理
 *
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-07-18
 */
@Service
@Transactional(readOnly = true)
public class ProductFilesServiceImpl extends BaseServiceImpl<ProductFilesMapper, ProductFiles> implements ProductFilesService {
    private static final Logger log = LoggerFactory.getLogger(ProductFilesServiceImpl.class);

    @Autowired
    private JinjianFilesService jinjianFilesService;

    /**
     * 获取单条数据
     *
     * @param productFiles 产品资料
     * @return 产品资料
     */
    @Override
    public ProductFiles get(ProductFiles productFiles) {
        ProductFiles dto = super.get(productFiles);
        return dto;
    }

    /**
     * 获取单条数据
     *
     * @param id 产品资料id
     * @return 产品资料
     */
    @Override
    public ProductFiles get(String id) {
        ProductFiles dto = super.get(id);
        return dto;
    }


    /**
     * 查询产品资料列表
     *
     * @param productFiles 产品资料
     * @return 产品资料
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public List<ProductFiles> findList(ProductFiles productFiles) {
        return super.findList(productFiles);
    }

    /**
     * 分页查询产品资料列表
     *
     * @param productFiles 产品资料
     * @return 产品资料
     */
    @Override
    @DataScope(userAlias = "a", deptAlias = "a")
    public PageInfo<ProductFiles> findPage(ProductFiles productFiles) {
        return super.findPage(productFiles);
    }

    /**
     * 保存产品资料
     *
     * @param productFiles
     * @return 结果
     */
    @Override
    public boolean save(ProductFiles productFiles) {
        return super.save(productFiles);
    }


    /**
     * 删除产品资料信息
     *
     * @param productFiles
     * @return 结果
     */
    @Override
    public boolean remove(ProductFiles productFiles) {
        return super.remove(productFiles);
    }

    /**
     * 批量删除产品资料
     *
     * @param ids 需要删除的产品资料ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteProductFilesByIds(String[] ids) {
        return mapper.deleteProductFilesByIds(ids);
    }

    /**
     * 获取最大编号
     *
     * @param productFiles 产品资料
     * @return 结果
     */
    @Override
    public int findMaxSort(ProductFiles productFiles) {
        return NumberUtils.nextOrder(mapper.findMaxSort(productFiles));
    }

    @Override
    public void deleteByProductId(String id) {
        mapper.deleteByProductId(id);
    }

    @Override
    public List<ProductFiles> findListByProductId(JinjianFiles jinjianFiles) {
        List<ProductFiles> productFilesList = mapper.findListByProductId(jinjianFiles.getProductId());

        for (ProductFiles dto : productFilesList) {
            // 查询资料
            JinjianFiles param = new JinjianFiles();
            param.setJinjianId(jinjianFiles.getJinjianId());
            param.setProductId(jinjianFiles.getProductId());
            param.setFilesName(dto.getFilesName());
            List<JinjianFiles> jinjianFilesList = jinjianFilesService.findList(param);
            List<Map<String, Object>> mapList = new ArrayList<>();
            if (!jinjianFilesList.isEmpty()) {
                JinjianFiles jinjianFilesOne = jinjianFilesList.get(0);
                Map<String, Object> map = new HashMap<>();
                map.put("uid", jinjianFilesOne.getId());
                map.put("name", jinjianFilesOne.getFilesName());
                map.put("status", "done");
                map.put("url", jinjianFilesOne.getFileUrl());
                mapList.add(map);
            }
            dto.setJinjianFilesList(mapList);
        }
        return productFilesList;
    }
}
